<?php

namespace App\Http\Middleware;

use Closure;

/*
 * 验证Token中间件
 * @author Jinfeihu
 * 
 */

class UserAuthenticate {

    use \App\Common\Traits\ResponseTrait;

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        //过滤不需要token的方法
        $actions = explode('@', $request->route()->getActionName());
        $controllerObject = $request->route()->getController();
        if (property_exists($controllerObject, 'actionConfig')) {
            if (in_array(end($actions), $controllerObject->actionConfig)) {
                return $next($request);
            }
        }
        //request()->headers->get('Authorization');
//        if (!$request->accessToken) {
//            return $this->errors(EMPTY_DATA, ACCESSTOKEN_ERRORS, 401);
//        }
        $auth = app('MemberService')->userInfo();
        if (!$auth) {
            return $this->errors("", AUTHENTICATION_ERROR, 400);
        }
        return $next($request);
    }

}
